package net.tngou.db;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.lucene.search.SortField;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.select.WithItem;
import net.sf.jsqlparser.statement.update.Update;
import net.tngou.db.entity.Field;
import net.tngou.db.entity.Field.Type;
import net.tngou.db.entity.Limit;
import net.tngou.db.util.ResultSet;

public class Test {

	
	public static void main(String[] args) throws JSQLParserException {

		CCJSqlParserManager parserManager = new CCJSqlParserManager();
		String statement = "select * from tngou where id='1' and title:text='天狗' and message:text='NoSQL' order by id ASC limit 1";
		Statement stat = parserManager.parse(new StringReader(statement));
		if(stat instanceof Select)
		{
			Select select =(Select) stat;
			
			PlainSelect s = (PlainSelect) select.getSelectBody();
			
			List<String> tables= new ArrayList<String>();
			tables.add(((Table)s.getFromItem()).getName()); //表
			List<Join> jts = s.getJoins();  //级联的表
			if(jts!=null)
			{
			for (Join join : jts) {
				tables.add(((Table)join.getRightItem()).getName());
				}
			}
//			tables.forEach(System.out::println);
			
			List<String> items= new ArrayList<String>();
			List<SelectItem> sitems = s.getSelectItems();
			
			sitems.forEach(e->{
				items.add(e.toString());
			});
//			items.forEach(System.out::println);
			
			Limit limit = new Limit();	  //limit 取数据
			net.sf.jsqlparser.statement.select.Limit lit = s.getLimit();	
			if(lit!=null)
			{
				limit.setStart((int) lit.getOffset());
				limit.setSize((int) lit.getRowCount());	
			}
			
			List<SortField> sortFields = new ArrayList<SortField>(); //order by 排序
			List<OrderByElement> orders = s.getOrderByElements();
			if(orders!=null)
			{
				orders.forEach(e->{
					OrderByElement order=e;
					String name = order.getExpression().toString();
					org.apache.lucene.search.SortField.Type type = org.apache.lucene.search.SortField.Type.LONG;
					boolean reverser = true; //倒叙
					if(order.isAsc() )reverser =false;
					SortField sortField = new SortField(name,type, reverser);
					sortFields.add(sortField);
				});
			}
			
			List<Field> wheres = new ArrayList<Field>();
			Expression where = s.getWhere();
			if(where!=null)
			{
				if(where instanceof EqualsTo)
				{
					EqualsTo equalsTo = (EqualsTo) where;
					 wheres.add(_getField(equalsTo));
				}
				else if(where instanceof AndExpression)
				{
				 
					AndExpression expression =(AndExpression) where;
				
					
					Expression left =null;
					Expression right=null;
					do {
					
						left = expression.getLeftExpression();
						right = expression.getRightExpression();
						if(right instanceof EqualsTo)
						{
							EqualsTo equalsTo = (EqualsTo) right;
							wheres.add(_getField(equalsTo));
						}
						if(left instanceof EqualsTo)
						{
							EqualsTo equalsTo = (EqualsTo) left;
							wheres.add(_getField(equalsTo));
						}else
						{
							expression=(AndExpression) left;
						}
						
						
						
					} while (left instanceof AndExpression);
					
				
				
				}
			}
				
			wheres.forEach(System.out::println);
			
		}
		
		
		
	
		
    }
	
	private static Field _getField(EqualsTo equalsTo) {

		String[] names = equalsTo.getLeftExpression().toString().split(":");
		Type type= Type.String;
		if(names.length>1)
		{
			switch (names[1].toLowerCase()) {
			case "key":
				 type= Type.Key;
				break;
			case "string":
				 type= Type.String;
				break;
			case "text":
				 type= Type.Text;
				break;
			default:
				type= Type.String;
				break;
			}
		}
		String value=((StringValue)equalsTo.getRightExpression()).getValue();
		Field field = new Field(names[0], value, type);
		return field;
	}
}
